package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.DeviceRepairInfo;
import com.ruoyi.mes.domain.ProductionLine;
import com.ruoyi.mes.mapper.DeviceRepairInfoMapper;
import com.ruoyi.mes.mapper.ProductionLineMapper;
import com.ruoyi.mes.utils.RepeatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.DeviceMapper;
import com.ruoyi.mes.domain.Device;
import com.ruoyi.mes.service.IDeviceService;

/**
 * 设备Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-11
 */
@Service
public class DeviceServiceImpl implements IDeviceService
{
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceRepairInfoMapper deviceRepairInfoMapper;
    @Autowired
    private ProductionLineMapper productionLineMapper;
    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Device selectDeviceById(Long id)
    {
        return deviceMapper.selectDeviceById(id);
    }

    /**
     * 查询设备列表
     *
     * @param device 设备
     * @return 设备
     */
    @Override
    public List<Device> selectDeviceList(Device device)
    {   List<Device> list = deviceMapper.selectDeviceList(device);
        for (Device device1 : list) {
            ProductionLine productionLine = productionLineMapper.selectProductionLineByLineId(device1.getLineId());
            device1.setLineName(productionLine.getLineName());
        }

        return list;
    }

    /**
     * 新增设备
     *
     * @param device 设备
     * @return 结果
     */
    @Override
    public AjaxResult insertDevice(Device device)
    {
        //设备编码判重
        if (RepeatUtils.isDeviceCodeRepeat(deviceMapper,device)){
            return AjaxResult.error("设备编码重复!");
        }



        device.setCreateTime(DateUtils.getNowDate());
        deviceMapper.insertDevice(device);
        return AjaxResult.success("新增成功!");
    }

    /**
     * 修改设备
     *
     * @param device 设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(Long[] ids)
    {
        return deviceMapper.deleteDeviceByIds(ids);
    }

    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceById(Long id)
    {
        return deviceMapper.deleteDeviceById(id);
    }

    /**
     * 查 - 维修记录
     * @param id 设备id
     * @return
     */
    @Override
    public AjaxResult getRepairInfoById(Long id) {
        List<DeviceRepairInfo> list = deviceRepairInfoMapper.getDeviceRepairInfoByDeviceId(id);
        return AjaxResult.success("查询成功!",list);
    }
}
